package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.mes.PartLog;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logPart.LogPartReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.logPart.LogPartResVo;
import com.zmj.sy.mom.srv.aps.mapper.PartLogMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@RequiredArgsConstructor
@Service
public class LogPartService {

    private final PartLogMapper partLogMapper;

    @SneakyThrows
    public BasePageResVo<LogPartResVo> log(LogPartReqVo reqVo) {

        Page<PartLog> page = partLogMapper.lambdaQuery()
                .eq(reqVo.getOperationType() != null, PartLog::getOperationType, reqVo.getOperationType())
                .eq(StringUtils.hasText(reqVo.getPartId()), PartLog::getPartId, reqVo.getPartId())
                .eq(reqVo.getPartType() != null, PartLog::getPartType, reqVo.getPartType())
                .like(StringUtils.hasText(reqVo.getPhaseCode()),PartLog::getPhaseCode,reqVo.getPhaseCode())
                .like(StringUtils.hasText(reqVo.getLoadId()),PartLog::getLoadId,reqVo.getLoadId())
                .like(StringUtils.hasText(reqVo.getGroupCode()),PartLog::getGroupCode,reqVo.getGroupCode())
                .like(StringUtils.hasText(reqVo.getStationCode()),PartLog::getStationCode,reqVo.getStationCode())
                .like(StringUtils.hasText(reqVo.getUserCode()),PartLog::getUserCode,reqVo.getUserCode())
                .like(StringUtils.hasText(reqVo.getNextPhaseCode()),PartLog::getNextPhaseCode,reqVo.getNextPhaseCode())
                .eq(reqVo.getReportId() != null, PartLog::getReportId, reqVo.getReportId())
                .orderByDesc(PartLog::getModifyDateTime)
                .page(reqVo.toPage());

        return BasePageResVo.of(page, LogPartResVo.class);
    }

}
